<?php
if( !class_exists('px_controller') ){
	class px_controller{
		var $vars;
		var $cur_gallery_info = NULL;

		function px_controller(){
			global $px_model;
			$this->init();
			$this->getOptions();
			$this->get_plugins();
		}
		
		function init(){
			global $px_view, $px_model;
			add_action('activate_' . $px_view->dirName . '/photoxhibit.php',array(&$px_model,'install'));
			add_action('deactivate_' . $px_view->dirName . '/photoxhibit.php',array(&$px_model,'unintall'));
		}
		
		function upgrade_init(){
			
		}
		
		function checkForGD(){
			if (function_exists("gd_info")) {
				$gd = gd_info();        
				foreach ($gd as $k => $v) {
					echo '<li>'. $k;
			
					if(is_numeric( $v ) || is_bool( $v )){
						if ($v){
							echo ' - <span style="color: #00AA00; font-weight: bold;"> Yes</span>';
						}
						else{
							echo ' - <span style="color: #EE0000; font-weight: bold;"> No</span>';
						}
					}
					else{
							echo ' - <span style="color: #00AA00; font-weight: bold;"> '.$v.'</span>';
					}
				}
			
			} else {
				echo '<li><span style="color: #EE0000; font-weight: bold;">not supported</span> by your server!</li>';
			}
		}
		
		function encodeTo($string) {		
			$charset = 'ASCII, UTF-8, ISO-8859-1, ISO-8859-15, JIS, EUC-JP, SJIS';
			if ( function_exists('mb_convert_encoding') ) {
				$string     = mb_convert_encoding($string, get_option('blog_charset'), $charset);
			}	
			return $string;
		}
		
		function getOptions(){
			global $px_model;
			$this->vars = $px_model->getOptions();
		}
		
		function setOptions(){
			$flickr_user_id = $_GET['flickr_user_id'];
			$flickr_api_key = $_GET['flickr_api_key'];
			$flickr_photoset_id = $_GET['flickr_photoset_id'];
			$picasa_user_id = $_GET['picasa_user_id'];
			$smugmug_api_key = $_GET['smugmug_api_key'];
			$smugmug_user_id = $_GET['smugmug_user_id'];
			
			
			$options = get_option('photoxhibit');
			$options['flickr_user_id'] = $flickr_user_id;
			$options['flickr_api_key'] = $flickr_api_key;
			$options['flickr_photoset_id'] = $flickr_photoset_id;
			$options['picasa_user_id'] = $picasa_user_id;
			$options['smugmug_api_key'] = $smugmug_api_key;
			$options['smugmug_user_id'] = $smugmug_user_id;
			$options["use_picasa"] = $_GET['use_picasa'];
			$options["use_flickr"] = $_GET['use_flickr'];
			$options["use_smugmug"] = $_GET['use_smugmug'];
			$options["use_album"] = $_GET['use_album'];
			$options["use_local"] = $_GET['use_local'];
			$options["use_browse"] = $_GET['use_browse'];
			
			$options["options_path"] = $_GET['options_path'];
			$options["options_delete"] = $_GET['options_delete'];
			
			$options["options_dropall"] = $_GET['options_dropall'];
			
			$options["options_MaxWidth"] = $_GET['options_MaxWidth'];
			$options["options_MaxHeight"] = $_GET['options_MaxHeight'];
			$options["options_imageQuality"] = $_GET['options_imageQuality'];
			
			$options["options_thumbailSet"] = $_GET['options_thumbailSet'];
			
			$options["options_thumbailW"] = $_GET['options_thumbailW'];
			$options["options_thumbailH"] = $_GET['options_thumbailH'];
			$options["options_thumbailW2"] = $_GET['options_thumbailW2'];
			$options["options_thumbailH2"] = $_GET['options_thumbailH2'];
			
			$options["options_tnimageQuality"] = $_GET['options_tnimageQuality'];
			$options["options_original"] = $_GET['options_original'];
	
			$options["use_effectSlide"] = $_GET['use_effectSlide'];
			$options["use_manager"] = $_GET['use_manager'];
			$options["flash_version_multi_upload"] = $_GET['flash_version_multi_upload'];
			$options["none_ajax_styles"] = $_GET['none_ajax_styles'];
			
			$options['plugin_cache'] = array();

			update_option('photoxhibit', $options);
		}
		
		function get_plugins(){
			global $px_view;
			$plugin_root = $px_view->dirPath . '/plugins/';
			$plugins_dir = @ opendir( $plugin_root );
			
			if ( $plugins_dir ) {
				while (($file = readdir( $plugins_dir ) ) !== false ) {
					if ( substr($file, 0, 1) == '.' )
						continue;
					if ( is_dir( $plugin_root.'/'.$file ) ) {
						$plugins_subdir = @ opendir( $plugin_root.'/'.$file );
						if ( $plugins_subdir ) {
							while (($subfile = readdir( $plugins_subdir ) ) !== false ) {
								if ( substr($subfile, 0, 1) == '.' )
									continue;
								if ( substr($subfile, -4) == '.txt' )
									$plugin_files[] = "$file/$subfile";
							}
						}  // end : if ( $plugins_subdir )
					}  // end : if ( is_dir( $plugin_root.'/'.$file ) )
				}  // end : while (($file = readdir( $plugins_dir ) ) !== false ) 
			}  // end : if ( $plugins_dir )
			
			@closedir( $plugins_dir );
			@closedir( $plugins_subdir );
			
			$px_plugins = array();
		
		
			foreach ( $plugin_files as $plugin_file ) {
				if ( !is_readable( "$plugin_root/$plugin_file" ) )
					continue;
		
				$plugin_data = $this->get_functionality_data( "$plugin_root/$plugin_file" );
		
				if ( empty ( $plugin_data['Title'] ) )
					continue;
				
				$px_name = explode( '/', $plugin_file);
				
				$px_plugins[ $px_name[0] ] = $plugin_data;
		
			}  //  end : foreach ( $plugin_files as $plugin_file )
			
			$this->vars['plugin_cache'] = $px_plugins;
			
		}  //  end : function get_plugins()
		
		function get_functionality_data( $plugin_file ) {
			$plugin_data = implode( '', file( $plugin_file ) );
			preg_match( '|Title:(.*)$|mi', $plugin_data, $plugin_name );
			preg_match( '|Css Path:(.*)$|mi', $plugin_data, $css_path );
			preg_match( '|Js Path:(.*)$|mi', $plugin_data, $js_path );
			preg_match( '|Example:(.*)$|mi', $plugin_data, $example );
			preg_match( '|Params:(.*)$|mi', $plugin_data, $params );
			preg_match( '|Plugin URI:(.*)$|mi', $plugin_data, $plugin_uri );
			preg_match( '|Description:(.*)$|mi', $plugin_data, $description );
			preg_match( '|Return Format:(.*)$|mi', $plugin_data, $return_format );
			preg_match( '|Dependencies:(.*)$|mi', $plugin_data, $dependencies );
		
			return array(
						'Title' => trim( $plugin_name[1] ),
						'Css' => trim( $css_path[1] ),
						'Js' => trim( $js_path[1] ),
						'Example' => trim( $example[1] ),
						'Params' => $this->jsonDecode( trim( $params[1] ) ),
						'Uri' => trim( $plugin_uri[1] ),
						'Description' => trim( $description[1] ),
						'Format' => trim( $return_format[1] ),
						'Dependencies' => trim( $dependencies[1] ),
						'Path' => str_replace('//', '/', basename(dirname( $plugin_file )) )
						);
		}  //  end : function get_functionality_data( $plugin_file )
		
		/**
		 *	Get Album JSON
		 */
		function get_json($url = NULL){
			$url = str_replace(array('rss_200','rss'),'json',$url);
			$return = @file_get_contents($url);

			return  ($return) ? $return : '{"result":"error","errorType":"fileGetContent"}';
		}
		function get_picasa_album_url($uid){
			return "http://picasaweb.google.com/data/feed/api/user/".$uid."?kind=album&alt=json";
		}
		function get_picasa_img_url($uid, $aid){
			return "http://picasaweb.google.com/data/feed/api/user/".$uid."/album/".$aid."?kind=photo&alt=json";
		}
		
		function jsonDecode($jsonD = NULL, $echo = false){
			if( $jsonD == NULL ) return false;

			$jsonD = stripslashes($jsonD);

			if (!extension_loaded('json')){
				include('JSON.php');
				$json = new JSON;
				
				if( $echo )
					echo $json->unserialize($jsonD);
				else
					return $json->unserialize($jsonD);
			}
			else{
				if( $echo )
					echo json_decode($jsonD);
				else
					return json_decode($jsonD);
			}
		}
		
		function jsonencode($jsonD = NULL, $echo = false){
			if( $jsonD == NULL ) return false;

			if (!extension_loaded('json')){
				include('JSON.php');
				$json = new JSON;
				
				if( $echo )
					echo $json->serialize($jsonD);
				else
					return $json->serialize($jsonD);
			}
			else{
				if( $echo )
					echo json_encode($jsonD);
				else
					return json_encode($jsonD);
			}
		}

	}//end : class h_core
}//end : if( !class_exists('px_controller') )
?>